package fr.ginvest.implementation;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import fr.ginvest.entity.Investisseur;
import fr.ginvest.entity.Participation;
import fr.ginvest.entity.Participation.ETAT;
import fr.ginvest.entity.Startup;
import fr.ginvest.interfaces.ParticipationFacadeRemote;

/**
 * Session Bean implementation class ParticipationFacade
 */
@Stateless

public class ParticipationFacade implements ParticipationFacadeRemote {

	@PersistenceContext
	protected EntityManager entityMgr;
	public ParticipationFacade() {
	}	

	public ParticipationFacade(EntityManager entityMgr) {
		this.entityMgr = entityMgr;
	}

	
    public Participation findParticipationById(int id) {
        return entityMgr.find(Participation.class, id);
    }
    
    public void deleteParticipation(Participation p) {
    	entityMgr.remove(p);
    }
    
    public Participation updateParticipation(Participation p) {
    	return entityMgr.merge(p);
    }
    
    public Participation addParticipation(Participation p) {
    	entityMgr.persist(p);
		return p;
    }

	@SuppressWarnings("unchecked")
	@Override
	public Collection<Participation> listParticipationSF(Investisseur fondateur) {
		String queryString = "select p.startup from Investisseur i inner join i.participations p where p.investisseur=:fondateur";
		Query query = entityMgr.createQuery(queryString).setParameter("fondateur", fondateur);
		List<Startup> startups = query.getResultList();
		Collection<Participation> participations = new ArrayList<Participation>();
		if(startups.size()>0){
			String queryString2 = "select p from Participation p where p.startup in (?1";
			for(int i=1; i<startups.size(); i++) {
				queryString2 += ",?" + (i+1);
			}
			queryString2 += ") ORDER BY p.startup";
			Query query2 = entityMgr.createQuery(queryString2);
			for(int i=0; i<startups.size(); i++) {
				query2.setParameter(i+1, startups.get(i));
			}
			participations = query2.getResultList();
		} 
		return participations;
	}

	@Override
	public void validateParticipation(Participation p) {
		p.setEtat(ETAT.VALIDEE);
		entityMgr.merge(p);
	}

	@SuppressWarnings("unchecked")
	@Override
	public Collection<Participation> listParticipationByInv(Investisseur inv) {
		String queryString = "select p from Participation p where p.investisseur=:inv";
		Query query = entityMgr.createQuery(queryString).setParameter("inv", inv);
		return query.getResultList();
	}

	@SuppressWarnings("unchecked")
	@Override
	public Collection<Object[]> listParticipationInStartup(Startup startup) {
		String queryString = "select p.investisseur, sum(p.montant) from Participation p where p.startup=:startup and p.etat=:etat group by p.investisseur";
		Query query = entityMgr.createQuery(queryString).setParameter("startup", startup).setParameter("etat", Participation.ETAT.VALIDEE);
		List<Object[]> list = query.getResultList();
		double totalCapital = 0;
		for(Object[] e: list) {
			totalCapital += (Double)e[1];
		}
		if(totalCapital > 0) {
			java.text.NumberFormat percentFormat =java.text.NumberFormat.getPercentInstance(); 
			percentFormat.setMaximumFractionDigits(2);
			percentFormat.setMaximumIntegerDigits(3);
			for(int i=0;i<list.size();i++) {
				double percentage = (Double)list.get(i)[1]/totalCapital;
				Object[] o = {list.get(i)[0], percentFormat.format(percentage)};
				list.set(i, o);
			}
		}
		return list;
	} 
}
